#ifndef _BSP_CURRENT_H
#define _BSP_CURRENT_H
#include "driver/gpio.h"
#include "bsp_timer.h"
#include "esp_attr.h"
#include "esp_adc/adc_continuous.h"

#define CURRENT_MCPWM_FREQUENCY (100*1000)
#define CURRENT_MCPWM_TICKS 256
#define CURRENT_GPIO GPIO_NUM_8

#define HBRIDGE_CTRL_0 GPIO_NUM_6
#define HBRIDGE_CTRL_1 GPIO_NUM_7
#define CURRENT_OUTPUT_PIN_SEL  ((1ULL<<HBRIDGE_CTRL_0) | (1ULL<<HBRIDGE_CTRL_1))

#define TOTAL_CURRENT_INTENSITY_LEVEL	9
#define V_MAX   3300       // MA
#define R_CY    68
/* 电极脱落 */
#define TEAS_BREAK_CHANNEL    	ADC_CHANNEL_8
#define TEAS_ADC_ATTEN        	ADC_ATTEN_DB_11
#define SAMPLE_RATE         	10000               // 采样率10kHz
#define ADC_BUFFER_SIZE     	1024                // DMA缓冲区大小
#define ADC_READ_LEN   		 	256                

enum{
    CURRENT_MODE1 = 0,
    CURRENT_MODE2,
    CURRENT_MODE3,
    CURRENT_MODE4,
    CURRENT_MODE_MAX,
};

enum{
	THERAPEUTIC_HEAD_CONNECT = 0,
	THERAPEUTIC_HEAD_DISCONNECT,
};

enum{
	DL_BREAK = 0,
	CL_BREAK,
};

typedef void (*current_hbridge_step_tick_function)(void);
struct current_function{
	void (*tick_func)(void);
	void (*change_freq)(uint32_t lvl);
    void (*change_elec)(uint32_t malvl);
	void (*initial)();
	void (*start)(void);
	void (*stop)(void);
};

static inline void current_a_to_b(void)
{
	gpio_set_level(HBRIDGE_CTRL_1, 0);
	gpio_set_level(HBRIDGE_CTRL_0, 1);
}

static inline void current_b_to_a(void)
{
	gpio_set_level(HBRIDGE_CTRL_0, 0);
	gpio_set_level(HBRIDGE_CTRL_1, 1);
}

static inline void current_bridge_shutdown(void)
{
    gpio_set_level(HBRIDGE_CTRL_0, 0);
    gpio_set_level(HBRIDGE_CTRL_1, 0);
}






extern IRAM_ATTR bool my_timer2_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx);

void current_output(uint32_t ua);
void current_initial(void);
uint32_t current_trans_intensity(uint32_t level);
void current_set_intensity(uint32_t level);
void current_set_frequecy(uint32_t level);
void current_output_start(uint32_t mode, uint32_t freq_lvl, uint32_t mA_lvl, uint8_t music_vol);
void current_output_stop(void);
#endif
